package cn.e2win.biz.ds;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysAttach;
import cn.e2win.base.entity.SysLabel;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.BaseEmp;
import cn.e2win.biz.dbo.DsEmp;


@Service
public class DsService extends BaseService{
	

	@Autowired
	private DsDao dsDao;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseDAO initDao() {
		return dsDao;
	}

	public void pagerDs(Pager pager){
		dsDao.pagerDs(pager);
	}
	@Transactional
	public DsEmp editDs(DsEmp obj)  throws MyException{

		//新添加用户
		if(obj.getId()==null){
			if(commonService.getEmpByMobile(obj.getMobile()).size()!=0){
				throw new MyException("当前导师的手机号【" + obj.getMobile() + "】已注册，请检查后重试！");
			}
		}else{
			obj.setMobile(null);
		}
		//保存主图片

		if(obj.getIcon()!=null && obj.getIcon().getSize()>0){
			SysAttach attach = uploadFile(obj.getIcon());
			obj.setIconUrl(attach.getFilePath());
		}
		DsEmp dsEmp = (DsEmp)super.edit(obj);
		
		String clazz = dsEmp.getClass().getSimpleName();
		
		String labelIndustry = obj.getLabelIndustry();
		String labelFocusOn = obj.getLabelFocusOn();
		String labelHelpFor = obj.getLabelHelpFor();
		SysLabel labelTpl = new SysLabel();
		labelTpl.setEntityId(dsEmp.getId());
		labelTpl.setLabelType("labelIndustry");
		labelTpl.setEntityType(clazz);
		commonService.editLabel(labelIndustry, labelTpl);
		
		labelTpl.setLabelType("labelFocusOn");
		commonService.editLabel(labelFocusOn, labelTpl);
		
		labelTpl.setLabelType("labelHelpFor");
		commonService.editLabel(labelHelpFor, labelTpl);
		
		//保存用户
		if(obj.getId()==null){
			commonService.saveUser4Emp(dsEmp);
		}
		return obj;
		
	}
}
